#include <iostream>
#include <register_client/xregister_client.h>
#include <string>
#include <thread>
#include <chrono>
#include <XTools.h>
using namespace std;
int main(int argc,char** argv){
    cout<<"register_client"<<endl;
    unsigned short port=REGISTER_PORT;
    string ip = "127.0.0.1";
    if(argc>1)
        ip = argv[1];
    if(argc>2)
        port=atoi(argv[2]);
    XRegisterClient::Get()->MsgFuncCallback();
    XRegisterClient::Get()->setServerIP(ip.c_str());
    XRegisterClient::Get()->setServerPort(port);
    XRegisterClient::Get()->RegisterServer("test",20020,"127.0.0.1");
    XRegisterClient::Get()->waitConnected(3);
    // int c =0;
    // while (!XRegisterClient::Get()->isConnected())
    // {
    //     std::cout<<c++<<"\n";
    //     std::this_thread::sleep_for(std::chrono::milliseconds(10));
    // }
    XRegisterClient::Get()->GetServiceReq(nullptr);
    for (size_t i = 0; i < 100; i++)
    {
        XCOM_LOG_DEBUG("GetServiceReq");
        XRegisterClient::Get()->GetServiceReq(nullptr);
        auto servicemap = XRegisterClient::Get()->GetAllService();
        if(!servicemap){
            XCOM_LOG_DEBUG("!servicemap");
            std::this_thread::sleep_for(std::chrono::seconds(6));
            continue;
        }
        auto service = servicemap->mutable_service_map();
        for (auto it = service->begin(); it != service->end(); it++)
        {
            std::cout<<it->first<<"\n";
        }
        std::this_thread::sleep_for(std::chrono::seconds(6));
    }
    
    XRegisterClient::Get()->Wait();
}